package com.gowants.pems.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.License;
import com.aspose.words.SaveFormat;

/**
 * word转pdf工具类
 * 
 * @author YLW
 *
 */
public class WordToPdfUtil {
	public boolean getLicense() {
		boolean result = false;
		try {
//			InputStream is = this.getClass().getClassLoader().getResourceAsStream("\\license.xml");
//			License aposeLic = new License();
//			aposeLic.setLicense(is);
//			result = true;
			 // 凭证
            String licenseStr =
                    "<License>\n"
                    + " <Data>\n"
                    + " <Products>\n"
                    + " <Product>Aspose.Total for Java</Product>\n"
                    + " <Product>Aspose.Words for Java</Product>\n"
                    + " </Products>\n"
                    + " <EditionType>Enterprise</EditionType>\n"
                    + " <SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
                    + " <LicenseExpiry>20991231</LicenseExpiry>\n"
                    + " <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>\n"
                    + " </Data>\n"
                    + " <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>\n"
                    + "</License>";
            InputStream license = new ByteArrayInputStream(licenseStr.getBytes("UTF-8"));
            License asposeLic = new License();
            asposeLic.setLicense(license);
            result = true;
		} catch (Exception e) {
//			e.printStackTrace();
			try {
				new TxtExport().writeTxtFile(e.getMessage(), "/data/scms_jar/output.txt");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
			}
			return result;
		}
		return result;
	}

	public static void main(String[] args) throws Exception {
        WordToPdfUtil bean = new WordToPdfUtil();
        bean.word2Pdf2("E:\\test.doc","E:\\TEST.pdf");
        String path="E:\\test.docx";
//        bean.word2pdf("E:\\TEST.pdf", wordInput)
    }

	/**
	 * inpath: 输入word的路径，例如： C:\\TEST.doc outpath: 输出pdf的路径，例如： C:\\TEST.pdf
	 */
	public void word2Pdf2(String inpath, String outpath) throws Exception {
		if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
			System.out.println("非法------------");
			return;
		}

		long old = System.currentTimeMillis();
		File file = new File(outpath);

		FileOutputStream os = new FileOutputStream(file);

		// 解决乱码
		// 如果是windows执行，不需要加这个
		// TODO 如果是linux执行，需要添加这个*****
		String OS = System.getProperty("os.name").toLowerCase();
		if (OS.indexOf("windows") > -1) {

		} else {
			new FontSettings().setFontsFolder("/usr/share/fonts/Fonts/", true);
		}

		Document doc = new Document(inpath); // Address是将要被转化的word文档
		doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
		long now = System.currentTimeMillis();
		System.out.println("共耗时：" + ((now - old) / 1000.0) + "秒");
	}

	/**
	 * @param path      pdf输出路径
	 * @param wordInput word输入流
	 */
	public boolean word2pdf(String path, InputStream wordInput) throws Exception {
		try {
			if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
				System.out.println("非法");
				return false;
			}
			File file = new File(path); // 新建一个空白pdf文档
			File fileParent = file.getParentFile();

			if (!fileParent.exists()) {
				fileParent.mkdirs();
			}
			FileOutputStream os = new FileOutputStream(file);
			// 解决乱码
			// 如果是windows执行，不需要加这个
			// TODO 如果是linux执行，需要添加这个*****
			String OS = System.getProperty("os.name").toLowerCase();
			if (OS.indexOf("windows") > -1) {

			} else {
				new FontSettings().setFontsFolder("/usr/share/fonts/Fonts/", true);
			}
			Document doc = new Document(wordInput);
			doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
		} catch (Exception e) {
//            e.printStackTrace();
			TxtExport.writeTxtFile(e.getMessage(), "/data/scms_jar/output.txt");
			return false;
		}

		return true;
	}
}
